<?php

namespace App\Service\UserService;

use App\Service\BaseService;
use App\Model\CommodityModel\SupplierCompany;
use App\Service\Code\Normal;
use App\Service\VendorClient\ShopCaiNiao\ShopCaiNiaoCcApiClient;
use App\Service\AppService\SystemCacheConfigService;
/**
 * 菜鸟备案
 * @author 陈韵
 */
class CaiNiaoService extends BaseService
{
    protected $cainiaoApiClient;
    protected $extends;
    /**
     * 接收消息数据
     * @param string $messageType
     * @param array $data
     */
    public function MessageRev(string $messageType,string $supplierId,array $data)
    {
        if( !getIsTomoMode())  {
            $config = $this->getConfig($supplierId);
            //如果没有配置就暂停查询
            if ($config['code'] != Normal::SUC)
            {
                return $config;
            }
            $this->cainiaoApiClient = new ShopCaiNiaoCcApiClient($config["data"]["JsonBody"]);
            return  $this->cainiaoApiClient->$messageType($data);
        }else{
            $config = $this->getTomoConfig($messageType);
            //如果没有配置就暂停查询
            if ($config['code'] != Normal::SUC)
            {
                return $config;
            }
            $class  = $config["data"]["class"]; 
            $param = [
                'JsonBody'      => $config["data"]["JsonBody"],
                'supplierGoods' => $data,
            ];
            return (new $class)->$messageType($param);
        }
    }
    
    private function getConfig($supplierId)
    { 
        //查询菜鸟配置
        $supplierInfo = SupplierCompany::instance()->getSupplierListById($supplierId);
        if(empty($supplierInfo) or empty($supplierInfo["JsonBody"])){
            return Normal::returnCode(Normal::FAIL,"没有查询到菜鸟的配置，请核实！");
        }
        return Normal::returnCode(Normal::SUC,"处理完成",$supplierInfo);
    }
    
    private function getTomoConfig(string $messageType)
    {
        //运单申报方式
        $declare_way_config = SystemCacheConfigService::instance()->getCacheInfoBySystemCode('tomo_waybill');
        if( empty($declare_way_config['mode']) ) {
            return Normal::returnCode(Normal::FAIL,"未配置运单服务商申报方式，请去中台系统-系统管理-业务设置-TOMO运单服务商配置！");
        }
        $mode = $declare_way_config['mode'];
        $class   = "App\\Service\\OrderService\\OrderRouteService\\{$mode}";
        if( !class_exists($class) ) {
            return Normal::returnCode(Normal::FAIL,"当前申报服务{$class}不存在，请联系管理员核实!");
        }
        if( !method_exists($class, $messageType) ) {
            return Normal::returnCode(Normal::FAIL,"当前申报服务{$class}方法{$messageType}不存在，请联系管理员核实!");
        }
        $client_way_config = SystemCacheConfigService::instance()->getCacheInfoBySystemCode($mode);
        if( empty($client_way_config) ) {
            return Normal::returnCode(Normal::FAIL,"未配置运单服务商{$mode}申报方式，请去中台系统-系统管理-业务设置-{$mode}");
        }
        $config = [
            'JsonBody' => $client_way_config,
            'class'    => $class,
        ];
        return Normal::returnCode(Normal::SUC,"处理完成",$config);
    }
}